package com.frxuexi.sign.view

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.DateTimeUtils
import com.frxuexi.sign.SignAdapter
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignListBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.security.MessageDigest


@Route(path = "/sign/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding, SignIntent, SignViewModel>() {
    override val binding: ActivitySignListBinding by lazy {
        ActivitySignListBinding.inflate(
            layoutInflater
        )
    }
    override val vm: SignViewModel by viewModels<SignViewModel>()
    val signAdapter by lazy { SignAdapter() }
    val list = mutableListOf<SignBeanItem>()

    var status = "全部状态"

    var flag=false

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as Collection<SignBeanItem>)
                        signAdapter.apply {
                            data.clear()
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.signRv.apply {
            adapter = signAdapter
            layoutManager = LinearLayoutManager(this@SignListActivity)
        }

        binding.signStatusTab.apply {
            addTab(newTab().setText("全部状态"))
            addTab(newTab().setText("迟到"))
            addTab(newTab().setText("早退"))
            addOnTabSelectedListener(object : OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    status = tab?.text.toString()
                    tab?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_blue)

                    val date = binding.signDate.text.toString()
                    val split = date.split("至")
                    signAdapter.apply {
                        data.clear()
                        if (tab?.position == 0) {
                            addData(if (date.isEmpty()) list else list.filter {
                                it.morningCheckIn.split(
                                    " "
                                )[0] in split[0]..split[1]
                            })
                        } else {
                            addData(if (date.isEmpty()) list.filter { it.status == tab?.text } else list.filter {
                                it.status == tab?.text && it.morningCheckIn.split(
                                    " "
                                )[0] in split[0]..split[1]
                            })
                        }
                        notifyDataSetChanged()
                    }

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_white)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
            getTabAt(0)?.view?.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_blue)
        }

        binding.signFilter.setOnClickListener {
//            selectByDate()
            selectByRange()
        }


//        val md = MessageDigest.getInstance("MD5")
//        val string="tamboo${DateTimeUtils.getDateTime(System.currentTimeMillis())}lr666"
//        md.digest(string.toByteArray());
    }

    fun selectByRange(){
        showDateDialog {
            if(flag){
                binding.signDate.append("至${it}")
                val split = binding.signDate.text.toString().split("至")
                val list2 = mutableListOf<SignBeanItem>()
                if (status == "全部状态") {
                    list2.addAll(list.filter { it.morningCheckIn.split(" ")[0] in split[0]..split[1] })
                } else {
                    list2.addAll(list.filter { it.morningCheckIn.split(" ")[0] in split[0]..split[1] && it.status == status })
                }
                signAdapter.data.clear()
                signAdapter.addData(list2)
                signAdapter.notifyDataSetChanged()
            }else{
                binding.signDate.setText(it)
            }
            flag=!flag

        }
    }

    /**
     * 根据日期查询
     */
    fun selectByDate(){
        showDateDialog { date ->
            binding.signDate.setText(date)
            val list2 = mutableListOf<SignBeanItem>()
            if (status == "全部状态") {
                list2.addAll(list.filter { it.morningCheckIn.split(" ")[0] == date })
            } else {
                list2.addAll(list.filter { it.morningCheckIn.split(" ")[0] == date && it.status == status })
            }
            signAdapter.data.clear()
            signAdapter.addData(list2)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(SignIntent.getSings(SPUtils.getInstance().getInt(Const.UID, -1)))
    }

}